//
//  ViewController.swift
//  volumButtonPressDemo
//
//  Created by qings on 15/12/1.
//  Copyright © 2015年 pukaila. All rights reserved.
//

import UIKit
import AVFoundation
import AssetsLibrary
import MediaPlayer

class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!

    let audioSession = AVAudioSession.sharedInstance()
    let mpc : MPMusicPlayerController = MPMusicPlayerController()
    
    var count: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(animated: Bool) {
        // 把提示框设置到非可见区域
        let wrapperView = UIView(frame: CGRectMake(1000,-200,260,20))
//        let wrapperView = UIView(frame: CGRectMake(100,200,260,20))
        self.view.addSubview(wrapperView)
        let volumeView = MPVolumeView(frame: wrapperView.bounds)
        wrapperView.addSubview(volumeView)
        listenVolumeButton()
    }
    
    func setVolume(volume: Float){
        print("setVolume")
        mpc.setValue(volume, forKeyPath: "volume")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func listenVolumeButton(){
        audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
    }

    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        initVolume = ((UIApplication.sharedApplication().delegate) as! AppDelegate).returnSystemVolume();
//        print("inteVolume: \(initVolume)")
//        if initVolume == 0.0 {
//            print(">>>>>>>>>>>>>>Asdfsadf")
//        }
//        if initVolume == 1.0 {
//            print("><<<<<<>>>>>>Asdfsadf")
//        }

//        initVolume = audioSession.outputVolume
//        print("inteVolume: \(initVolume)")
        
//        //获得上一级系统声音
//        let mpc : MPMusicPlayerController = MPMusicPlayerController()
//        let systemVolume = ((UIApplication.sharedApplication().delegate) as! AppDelegate).returnSystemVolume()
//        
//        print("app systemVolume = \(systemVolume)")
//        
//        var tempVolume = systemVolume
//        if systemVolume == 0.0 || systemVolume == 1.0 {
//            tempVolume = 0.125
//        }
////        print("before tempVolume:\(tempVolume) tempVolume:\(audioSession.outputVolume) ")
//        mpc.setValue(tempVolume, forKeyPath: "volume")
////        print("after tempVolume:\(tempVolume) tempVolume:\(audioSession.outputVolume) ")
        //获得上一级系统声音
        
        if (!((UIApplication.sharedApplication().delegate) as! AppDelegate).isClose) {
            let appVolume = ((UIApplication.sharedApplication().delegate) as! AppDelegate).returnAppVolume()
            mpc.setValue(appVolume, forKeyPath: "volume")
            print("(%f %f)",audioSession.outputVolume, appVolume)
            if keyPath == "outputVolume"{
                if appVolume != audioSession.outputVolume {
                    count++
                    testLabel.text = NSString(format: "%02f : %02d", appVolume, count) as String
                    print("dodo shoot")
                }
            }
        }
    }

}

